package org.xdq;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;

public class Test {

    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {


        int count = 1000;

        Connection conn =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","root");

        conn.setAutoCommit(false);//设为手动提交事务，即开发者自定义事务边界

        String sql = "insert into emp(e_id,e_name,e_sex,e_date,e_sal) values(?,?,?,?,?)";
        PreparedStatement pst = conn.prepareStatement(sql);


        long start = System.currentTimeMillis();
        int i=0;
        while(i<count){

            pst.setString(1, UUID.randomUUID().toString().replaceAll("-", ""));
            pst.setString(2, String.valueOf((int)(Math.random()*999999999)));
            pst.setInt(3, (int)(Math.random()*2+1));
            long time = (long)(20*365*24*3600*1000*Math.random());
            pst.setDate(4, new java.sql.Date(System.currentTimeMillis()-time));
            pst.setDouble(5, 2000+9000*Math.random());

            pst.addBatch();

            //pst.executeUpdate();

            i++;

        }

        pst.executeBatch();
        conn.commit();

        System.out.println("耗时："+(System.currentTimeMillis()-start));

        pst.clearBatch();//清除批语句

        pst.close();
        conn.close();

    }



}
